import java.nio.FloatBuffer;
import com.sun.opengl.util.*;

class BallSystem {

  final static int maxBalls = 3;
  int curIndex;

  Ball[] balls;

  BallSystem() {
    balls = new Ball[maxBalls];
    //for(int i=0; i<maxBalls; i++) balls[i] = new Ball();
    curIndex = 0;
  }

  void fadeToColor(GL gl, float r, float g, float b, float speed) {
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4f(r, g, b, speed);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0, 0);
    gl.glVertex2f(width, 0);
    gl.glVertex2f(width, height*.95);
    gl.glVertex2f(0, height*.95);
    gl.glEnd();
  }

  void fadeBottom(GL gl) {
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4f(0,0,0,1);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0,height*.95);
    gl.glVertex2f(width, height*.95);
    gl.glVertex2f(width,height);
    gl.glVertex2f(0,height);
    gl.glEnd();
  }

  void updateAndDraw(){
    PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;         // processings opengl graphics object
    GL gl = pgl.beginGL();
    gl.glEnable(GL.GL_BLEND);
    fadeToColor(gl,0,0,0,0.05);
    fadeBottom(gl);
    gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
    gl.glEnable(GL.GL_LINE_SMOOTH);
    gl.glLineWidth(1);
    gl.glBegin(GL.GL_QUADS);
    for(int i=0; i<curIndex; i++) {
      if(balls[i].alpha > 0) {
        balls[i].update();
        balls[i].drawOldSchool(gl);
      }
    }
    gl.glEnd();
    gl.glDisable(GL.GL_BLEND);
    pgl.endGL();
  }

  void addBall() {
    if(curIndex >= maxBalls) return;
    balls[curIndex] = new Ball();
    balls[curIndex].init(mouseX,mouseY);
    curIndex++;
    //if(curIndex >= maxBalls) curIndex = maxBalls-1;
  }

  void addBall(int x, int y) {
    if(curIndex >= maxBalls) return;
    balls[curIndex] = new Ball();
    balls[curIndex].init(x,y);
    curIndex++;
    //if(curIndex >= maxBalls) curIndex = maxBalls-1;
  }

  void removeBall() {
    if (curIndex == 0) return;
    curIndex--;
    balls[curIndex] = null;
  }

  //Currently Un-Implemented
  /*void removeBall(int i) {
   if(curIndex == 0) return;
   else if(curIndex == maxBalls-1)
   removeball();
   else {*/

}


